$(function() {
	
	$('#userform').validate({
		rules: {
			loginName: {
				loginName: true,
				checkName: true
			},
			name: {
				nickname: true
			},
			plainPassword: {
				password: true
			}
		}
	});
	
	$("#userSubmit").click(function(){
		if(!$("#userform").valid()) {
			return false;
		}
		var roleIds = '';
		$('#roleId option:selected').each(function(i){
			roleIds += $(this).val() + ',';
		});
		$('#roleIds').val(roleIds);
		var params = $("#userform").serialize();
		$.ajax({
			data : params,
			type : 'POST',
			url :baselocation+'/system/user/add',
			success : function(result) {
				if (result.code==0) {
					var successMsg = "";
					var userId = $("#userId").val();
					if(userId == "") {
						successMsg = "添加用户成功";
					}else{
						successMsg = "修改用户成功";
					}
					
					layer.msg(successMsg, {
						icon : 1,
						time : 1000
					}, function() {
						window.parent.location.reload(); // 刷新父页面
					});
				} else {
					layer.msg(result.message, {
						icon : 2,
						time : 1000
					});
				}
			}
		});
		return false;
	});
});